package appx.craft.droid.uc;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import appx.craft.droid.R;
import appx.craft.droid.bean.FeedBean;
import appx.craft.droid.ui.SlideShowActivity;
import appx.craft.droid.utils.Constance;
import appx.craft.droid.utils.Log;

import com.nostra13.universalimageloader.core.ImageLoader;

public class FragmentSliderPager extends Fragment {

	public final String TAG = FragmentSliderPager.class.getCanonicalName();
	
	SlideShowActivity mCurrentActivity;
	int mNum;
	ImageView mPhotoView;
	ITapOnImageListener mTapCallback;
	
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	
	public static FragmentSliderPager newInstance(int Num) {
		final FragmentSliderPager mFragmentSliderPager = new FragmentSliderPager();
		final Bundle args = new Bundle();
		args.putInt(Constance.NUM_VIEWS, Num);
		mFragmentSliderPager.setArguments(args);
		return mFragmentSliderPager;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		Log.d(TAG, "[ onAttach ] ");
		mTapCallback = (ITapOnImageListener)activity;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "[ onCreate ] ");
		mNum = getArguments() != null ? getArguments().getInt(Constance.NUM_VIEWS) : -1;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.d(TAG, "[ onCreateView ] ");
		
		// Inflate and locate the main ImageView
		final View mView = inflater.inflate(R.layout.fragment_slidshow,container, false);
		mPhotoView = (ImageView)mView.findViewById(R.id.imageView_slide_photo);
		mPhotoView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mTapCallback.onTapImageView(mNum);
			}
		});
		
		FeedBean mPhotoPreviewBean = SlideShowActivity.mArrayListFeeds.get(mNum);
		if(mPhotoPreviewBean.getTypeID().equalsIgnoreCase(Constance.VIEW_UPLOAD)){
			 Bitmap bitmap = BitmapFactory.decodeFile( mPhotoPreviewBean.getFilePath());
			 mPhotoView.setImageBitmap(bitmap);
		}else if(mPhotoPreviewBean.getTypeID().equalsIgnoreCase(Constance.VIEW_DOWNLOAD)){
			String uri = mPhotoPreviewBean.getFilePath();
			Log.d(TAG,"[ File Path ]" + uri);
			imageLoader.displayImage(uri,mPhotoView);
		}
		
		
		
		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Log.d(TAG, "[ onActivityCreated ]");
	}

	@Override
	public void onStart() {
		super.onStart();
		Log.d(TAG, "[ onStart ] ");

	}

	@Override
	public void onResume() {
		super.onResume();
		Log.d(TAG, "[ onResume ] ");
	}

	@Override
	public void onPause() {
		super.onPause();
		Log.d(TAG, "[ onPause ] ");
	}

	@Override
	public void onStop() {
		super.onStop();
		Log.d(TAG, "[ onStop ] ");
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		Log.d(TAG, "[ onDestroyView ] ");
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "[ onDestroy ] ");
	}

	@Override
	public void onDetach() {
		super.onDetach();
		Log.d(TAG, "[ onDetach ] ");
	}


}
